package com.eastfair.visitor.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.visitor.dao.AuditSettingsMapper;
import com.eastfair.visitor.entity.AuditSettings;
import com.eastfair.visitor.entity.Department;
import com.eastfair.visitor.service.AuditSettingsService;
import com.eastfair.boot.service.SuperServiceImpl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import java.util.Collection;
//import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.aop.framework.AopContext;
import com.eastfair.annotation.annotation.cache.Magic;
import com.eastfair.annotation.annotation.cache.Cache;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
//import com.baomidou.dynamic.datasource.annotation.DS;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.List;
import com.eastfair.visitor.vo.*;
import com.eastfair.visitor.dto.*;
import org.springframework.transaction.annotation.Transactional;

/**
* <p>
    * 业务实现类
    * 审核设置表
    * </p>
*
* @author lyw
* @date 2022-11-18
*/
@Slf4j
@Service
//@DS("#thread.tenant")
    public class AuditSettingsServiceImpl extends SuperServiceImpl<AuditSettingsMapper, AuditSettings> implements AuditSettingsService {

    @Autowired
    private AuditSettingsMapper auditSettingsMapper;


    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
    */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<AuditSettings> modelList) {
    modelList.stream().forEach(s -> {
    s.setIsDeleted(BusinessConstant.YES);
    });
    // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
    return R.successDef();
    }

    /**
    * 通用查询
    *
    * @param modelDTO DTO
    * @param ids 主键id集合
    * @return list
    */
    @Override
    public List<AuditSettings> list(AuditSettingsDTO modelDTO, List<Long> ids) {
        return ((AuditSettingsService) AopContext.currentProxy()).listOfCache(modelDTO, ids);
    }

    @Override
    protected R<Boolean> handlerSave(AuditSettings model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }


    /**
     * 缓存查询
     *
     * @param modelDTO DTO
     * @param ids 主键id集合
     * @return list
     */
    @Override
    @Cache(key = "",
       magic = @Magic(
       key = "{"+
       "{'switchName', #args[0]?.switchName},"+
       "{'isEnabled', #args[0]?.isEnabled},"+
        "{'id', #args[1]}"+
        "}"))
    public List<AuditSettings> listOfCache(AuditSettingsDTO modelDTO, List<Long> ids) {
       //查询
       QueryWrapper<AuditSettings> queryWrapper = new QueryWrapper<>();
       if (modelDTO != null) {
                    queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getSwitchName()),AuditSettings::getSwitchName,modelDTO.getSwitchName());
            }
            if (ids != null && !ids.isEmpty()) {
            queryWrapper.lambda().in(AuditSettings::getId, ids);
            }
            queryWrapper.lambda().eq(AuditSettings::getIsDeleted, BusinessConstant.DELETE_NO);
            List<AuditSettings> modelList = list(queryWrapper);
            return modelList;
    }

    /**
     * 审核开关列表
     * @param params
     * @return
     */
    @Override
    public IPage<AuditSettingsVO> queryDepartmentList(PageParams<AuditSettingsPageQuery> params) {

        return null;
    }

    /**
     * 审核开关列表
     * @param auditSettingsPageQuery
     * @return
     */
    @Override
    public List<AuditSettingsVO> getAuditSettingsList(AuditSettingsPageQuery auditSettingsPageQuery) {
        log.info("AuditSettingsServiceImpl +++++ getAuditSettingsList - 审核开关列表 params={}", JSONObject.toJSON(auditSettingsPageQuery));
        QueryWrapper<AuditSettings> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(AuditSettings::getIsDeleted, BusinessConstant.DELETE_NO)
                .like(StrUtil.isNotBlank(auditSettingsPageQuery.getSwitchName()),
                        AuditSettings::getSwitchName, auditSettingsPageQuery.getSwitchName())
                .orderByDesc(AuditSettings::getCreateTime);
        List<AuditSettings> list = list(queryWrapper);
        List<AuditSettingsVO> auditSettingsVOList = ConvertUtil.convertList(list, AuditSettingsVO.class);
        return auditSettingsVOList;
    }

    /**
     * 查询开关信息
     * @return
     */
    @Override
    public AuditSettingsVO getAuditSettingsInfo() {
        QueryWrapper<AuditSettings> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(AuditSettings::getIsDeleted, BusinessConstant.DELETE_NO);
        AuditSettings auditSettings = this.getOne(queryWrapper);
        AuditSettingsVO auditSettingsVO = BeanUtil.toBean(auditSettings, AuditSettingsVO.class);
        return auditSettingsVO;
    }

    /**
     * 添加审核开关
     * @param auditSettingsSaveDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveAuditSettingsInfo(AuditSettingsSaveDTO auditSettingsSaveDTO) {
        log.info("AuditSettingsServiceImpl +++++ saveAuditSettingsInfo - 添加审核开关 auditSettingsSaveDTO={}", JSONObject.toJSON(auditSettingsSaveDTO));
        AuditSettings auditSettings = BeanUtil.toBean(auditSettingsSaveDTO, AuditSettings.class);
        boolean save = save(auditSettings);
        return save;
    }

    /**
     * 修改审核开关
     * @param auditSettingsUpdateDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateAuditSettingsInfo(AuditSettingsUpdateDTO auditSettingsUpdateDTO) {
        log.info("AuditSettingsServiceImpl +++++ updateAuditSettingsInfo - 添加审核开关 auditSettingsUpdateDTO={}", JSONObject.toJSON(auditSettingsUpdateDTO));
        AuditSettings auditSettings = BeanUtil.toBean(auditSettingsUpdateDTO, AuditSettings.class);
        auditSettingsMapper.updateById(auditSettings);
        return Boolean.TRUE;
    }

    /**
     * 逻辑删除审核开关
     * @param auditSettingsUpdateDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delAuditSettingsById(AuditSettingsUpdateDTO auditSettingsUpdateDTO) {
        log.info("AuditSettingsServiceImpl +++++ delAuditSettingsById - 添加审核开关 auditSettingsUpdateDTO={}", JSONObject.toJSON(auditSettingsUpdateDTO));
        AuditSettings auditSettings = BeanUtil.toBean(auditSettingsUpdateDTO, AuditSettings.class);
        auditSettings.setIsDeleted(1);
        auditSettingsMapper.updateById(auditSettings);
        return Boolean.TRUE;
    }


    /**
     * do转vo
     * @param model model
     * @return modelVo
     */
    @Override
    public AuditSettingsVO doToVo(AuditSettings model) {
        AuditSettingsVO modelVo = new AuditSettingsVO();
        BeanUtils.copyProperties(model, modelVo);
        modelVo.setId(model.getId());
        return modelVo;
    }
    /**
     * do转vo
     * @param modelList modelList
     * @return list
     */
     @Override
     public List<AuditSettingsVO> doToVos(List<AuditSettings> modelList) {
        if (modelList == null || modelList.isEmpty()) {
        return null;
        }
        List<AuditSettingsVO> result = new ArrayList<>();
        for (AuditSettings model : modelList) {
           result.add(doToVo(model));
        }
        return result;
    }
}
